package com.kuang.bascic.controller;

import com.kuang.bascic.Myexception.BusinessException;
import com.kuang.bascic.service.BaseCodeService;
import com.kuang.bascic.service.IVerifyCodeService;
import com.kuang.bascic.utils.AjaxResult;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class CodeController {
    @Autowired
    private BaseCodeService baseCodeService;
    @Autowired
    private IVerifyCodeService verifyCodeService;

    @PostMapping("/sendCode")
    public AjaxResult sendCode(@RequestBody Map<String, String> map) {
        //这里接异常
        try {
            // 获取验证码
            baseCodeService.sentCode(map);
            return AjaxResult.me();
        } catch (BusinessException e) {
            // 自定义异常放在前面  不是这个异常在去抛大异常
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统异常");
        }
    }

    @GetMapping("/verifyCode/image/{key}")
    public String verifyCode(@PathVariable("key") String key) {
        return verifyCodeService.getImgCode(key);

    }

    @PostMapping("/emaiCode")
    public AjaxResult emailRes(@RequestBody Map<String, String> map) {
        //{phone=, codetype=REGINSTER, code=, password=, passwordRepeat=, email=1111}
        System.out.println(map);
        String email = map.get("email");
        String codetype = map.get("codetype");
        if (email.isEmpty()) {
            return AjaxResult.me().setMessage("输入为空");
        }
        baseCodeService.sentEmailCode(email,codetype);
        return AjaxResult.me();
    }
}
